<?php
namespace app\api\controller;

use think\Controller;
use app\api\model\Postdatadb;

class Base extends Controller
{

    protected $ys_config = [
        'geteway' => 'http://api.utecbj.com/erp/api/',
        'partnerId' => 25,
        'siteId' => 33,
        'appId' => 'utec',
        'appKey' => 'yserp',
        'appSecret' => 'duXtRelcYy2sJeqrLpmNvbQO0O0OO0O0O'
    ];

    protected $username = 'utec_tianjin';
    protected $password = '93bb74cfeadaabd4';
    protected $member_id = 46;
    protected $site_url = 'http://tj.mall.utecbj.com/';

    public $request = array();

    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");
        // header('Content-Type:application/json; charset=utf-8');
        $request = request();
        $this->request = $request->param();
        // 捕获请求的数据
        
        $postdatadb = new Postdatadb();
        $postdatadb->param['post_json'] = $this->request;
        $postdatadb->param['post_url'] = $request->url();
        $postdatadb->ajaxSave();
        
    }

    public function index()
    {
        exit('欢迎使用');
    }

    protected function sendInfo($string_uri, $post)
    {
        $post['config'] = $this->ys_config;
        $json_data =json_encode($post);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->ys_config['geteway'] . $string_uri);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Content-type: application/json;charset='utf-8'"
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        return json_decode($result, true);
    }

    protected function ajaxReturn($return, $json_option = 0)
    {
        header('Content-Type:application/json; charset=utf-8');
        exit(json_encode($return, $json_option));
    }
    protected function chkToken(){
        if(!isset($this->request['token'])){
            $return['success'] = false;
            $return['desc'] = '没有token';
            $this->ajaxReturn($return);
        }
        $post['token'] = $this->request['token'];
        $data = $this->sendInfo('chkToken', $post);
        if($data['code'] != 1){
            $return['success'] = false;
            $return['desc'] = $data['msg'];
            $this->ajaxReturn($return);
        }
    }
}